package com.cn.qgx428.orderingmeal.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.pm.ProviderInfo;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.qgx428.orderingmeal.R;
import com.cn.qgx428.orderingmeal.myClass.User;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by QinX on 2017/4/24.
 */

public class MineInfoDialog extends Dialog implements View.OnClickListener, View.OnFocusChangeListener {

    private static final String TAG = "MineInfoDialog";

    private Context context;

    private String userID;
    private User user;

    private EditText name;
    private EditText phone;
    private EditText address;

    private TextView cancel;
    private TextView submit;
    private TextView eidtBtn;

    private static final int EDITED = 0;
    private static final int NOT_EDITE = 1;
    private int EDITE_INFO = NOT_EDITE;
    public MineInfoDialog(@NonNull Context context, String userID) {
        super(context);
        this.context = context;
        this.userID = userID;
        Bmob.initialize(context,"1b925fe2ccbb402f4034a3e28c31ad93");
        setMineInfoDialog();
    }

    private void setMineInfoDialog() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.mine_mine_info,null);
        name = (EditText) view.findViewById(R.id.mine_mineName);
        phone = (EditText) view.findViewById(R.id.mine_minePhone);
        address = (EditText) view.findViewById(R.id.mine_mineAddress);
        eidtBtn = (TextView) view.findViewById(R.id.mine_mineInfoEdit);
        cancel = (TextView) view.findViewById(R.id.mine_mineCancel);
        submit = (TextView) view.findViewById(R.id.mine_mineSubmit);



        //获取用户数据
        getUserData();
        initEvent();
        setContentView(view);
    }

    private void initEvent() {
        //设置Edite文本光标位置
        name.setOnFocusChangeListener(this);
        phone.setOnFocusChangeListener(this);
        address.setOnFocusChangeListener(this);
        eidtBtn.setOnClickListener(this);
        cancel.setOnClickListener(this);
        submit.setOnClickListener(this);
    }

    private void getUserData() {
        BmobQuery<User> query = new BmobQuery<User>();
        query.getObject(userID, new QueryListener<User>() {
            @Override
            public void done(User u, BmobException e) {
                if (e == null){
                    user = u;
                    name.setText(u.getName());
                    phone.setText(u.getMobilePhoneNumber());
                    address.setText(u.getAddress());
                } else {
                    Log.d(TAG, "获取用户数据失败： "+e.getErrorCode()+e.getMessage());
                }
            }
        });
    }

    public MineInfoDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    protected MineInfoDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.mine_mineInfoEdit:
                makeEnableEdit();
                break;
            case R.id.mine_mineCancel:
                dismiss();
                break;
            case R.id.mine_mineSubmit:
                //更新用户信息
                updateUserInfo();
                break;
        }
    }

    private void makeEnableEdit() {
        Log.d(TAG, "onClick: "+"ye");
        //令用户信息文本可编辑
        name.setFocusableInTouchMode(true);
        phone.setFocusableInTouchMode(true);
        address.setFocusableInTouchMode(true);
        name.setFocusable(true);
        EDITE_INFO = EDITED;
    }

    private void updateUserInfo() {
        if (EDITE_INFO == NOT_EDITE){
            dismiss();
        } else if(EDITE_INFO == EDITED){
            //检查数据信息
            if ("".equals(name.getText().toString())){
                Toast.makeText(context, "昵称不能为空", Toast.LENGTH_SHORT).show();
            } else if ("".equals(phone.getText().toString())){
                Toast.makeText(context, "电话不能为空", Toast.LENGTH_SHORT).show();
            } else if ("".equals(address.getText().toString())){
                Toast.makeText(context, "送货地址不能为空", Toast.LENGTH_SHORT).show();
            } else if(phone.getText().length() != 11){
                Toast.makeText(context, "电话号码不正确", Toast.LENGTH_SHORT).show();
            } else{
                user.setAddress(address.getText().toString());
                user.setName(name.getText().toString());
                user.setMobilePhoneNumber(phone.getText().toString());
                user.update(user.getObjectId(), new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null){
                            Toast.makeText(context, "更新成功", Toast.LENGTH_SHORT).show();
                            dismiss();
                        } else {
                            Toast.makeText(context, "服务器错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        EditText _v = (EditText) v;
        if (hasFocus){
            _v.setSelection(_v.getText().length());
        }


    }
}
